import java.io.*;
import java.util.Scanner;

public class Demo63 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要搜素的文件目录");
        String rootPath = scanner.next();
        System.out.println("请输入要搜索的内容");
        String word = scanner.next();
        File fileDepath = new File(rootPath);
        if(fileDepath.isDirectory()){
            return;
        }
        scanfDir(fileDepath,word);
    }

    private static void scanfDir(File fileDepath, String word) {
       File[] files = fileDepath.listFiles();
       if(files==null){
           return;
       }
        for (File f:files) {
            if(f.isFile()){
                sear(f,word);
            } else if (f.isDirectory()) {
                scanfDir(f,word);
            }
        }
    }

    private static void sear(File f, String word) {
        try(InputStream inputStream = new FileInputStream(f)){
            StringBuffer stringBuffer = new StringBuffer();
            while(true) {
                byte[] buffer = new byte[1024];
                int n = inputStream.read(buffer);
                if (n == -1) {
                    break;
                }
               String s = new String(buffer,0,n);
                stringBuffer.append(s);
            }
            if(stringBuffer.indexOf(word) !=-1){
                System.out.println("找到了");
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
